//
//  FIUIUtility.m
//  Magpie
//
//  Created by 100TV protocal on 12-11-15.
//
//

#import "FIUIUtility.h"
#import <QuartzCore/QuartzCore.h>

@implementation FIUIUtility

@end

void alert(NSString* msg){
	UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

@interface YKAlertDelegateWrapper : NSObject<UIAlertViewDelegate>{
	id target;
	SEL action;
	int targetButtonIndex;	//点哪个button 会执行[target action];
}
@property(nonatomic,retain) id target;
@property(nonatomic,assign) SEL action;
@property(nonatomic,assign) int targetButtonIndex;

-(id) initWithTarget:(id) atarget action:(SEL)aaction;
-(id) initWithTarget:(id)atarget action:(SEL)aaction targetButtonIndex:(int)atargetButtonIndex;
@end

@implementation YKAlertDelegateWrapper
@synthesize target;
@synthesize action;
@synthesize targetButtonIndex;


-(void) internalInitWithTarget:(id)atarget action:(SEL)aaction targetButtonIndex:(int)atargetButtonIndex{
	assert(atarget!=nil);
	assert(aaction!=nil);
	self.target=atarget;
	self.action=aaction;
	self.targetButtonIndex=atargetButtonIndex;
}

-(id) initWithTarget:(id)atarget action:(SEL)aaction{
	if((self=[super init])){
		[self internalInitWithTarget:atarget action:aaction targetButtonIndex:0];
	}
	return self;
}

-(id) initWithTarget:(id)atarget action:(SEL)aaction targetButtonIndex:(int)atargetButtonIndex{
	if((self=[super init])){
		[self internalInitWithTarget:atarget action:aaction targetButtonIndex:atargetButtonIndex];
	}
	return self;
}
-(void) dealloc{
	[target release];
	[super dealloc];
}


-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	if(buttonIndex==targetButtonIndex){
		//确定
		[target performSelector:action];
	}else{
		//
	}
	[self autorelease];
}
@end

void alert2(NSString* msg, NSString* btnTitle1, NSString* btnTitle2, id target, SEL action){
	UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:btnTitle1 otherButtonTitles:btnTitle2,nil];
	if(target!=nil){
		YKAlertDelegateWrapper* delegate=[[YKAlertDelegateWrapper alloc] initWithTarget:target action:action];
		alert.delegate=delegate;
	}
	[alert show];
	[alert release];
}

void alert3(NSString* msg, NSString* btnTitle1, NSString* btnTitle2, id target, SEL action){
	UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:btnTitle1 otherButtonTitles:btnTitle2,nil];
	if(target!=nil){
		YKAlertDelegateWrapper* delegate=[[YKAlertDelegateWrapper alloc] initWithTarget:target action:action targetButtonIndex:1];
		alert.delegate=delegate;
	}
	[alert show];
	[alert release];
}




@implementation UIView(addbord_extension)

-(void) addBottomBorderWidth:(float) awidth{
	[self addBottomBorderWidth:awidth color:[UIColor lightGrayColor]];
}
-(void) addBottomBorderWidth:(float) awidth color:(UIColor*) acolor{
	assert(awidth>0);
	assert(acolor!=nil);
	CGRect f=CGRectMake(0, self.frame.size.height-awidth, self.frame.size.width, awidth);
	UIView* borderView=[[UIView alloc] initWithFrame:f];
	borderView.autoresizingMask=( UIViewAutoresizingFlexibleWidth  | UIViewAutoresizingFlexibleTopMargin  );
	borderView.backgroundColor=acolor;
	[borderView addBorderWidth:awidth color:acolor cornerRadius:0];
	[self addSubview:borderView];
	[borderView release];
}

-(void) addRightBorderWidth:(float) awidth{
	[self addRightBorderWidth:awidth color:[UIColor lightGrayColor]];
}
-(void) addRightBorderWidth:(float) awidth color:(UIColor*) acolor{
	assert(awidth>0);
	assert(acolor!=nil);
	CGRect f=CGRectMake(self.frame.size.width-awidth, 0, awidth, self.frame.size.height);
	////!! NSLog(@"f=%@ self.frame=%@",NSStringFromCGRect(f), NSStringFromCGRect(self.frame) );
	UIView* borderView=[[UIView alloc] initWithFrame:f];
	borderView.autoresizingMask=( UIViewAutoresizingFlexibleHeight  | UIViewAutoresizingFlexibleLeftMargin  );
	borderView.backgroundColor=acolor;
	[borderView addBorderWidth:awidth color:acolor cornerRadius:0];
	[self addSubview:borderView];
	[borderView release];
}

-(void) addBorderWidth:(float) awidth color:(UIColor*)acolor cornerRadius:(float) acornerRadius{
	self.layer.borderWidth=awidth;
	self.layer.borderColor=[acolor CGColor];
	self.layer.cornerRadius=acornerRadius;
}

-(void) addBorderWidth:(float) awidth cornerRadius:(float) acornerRadius{
	[self addBorderWidth:awidth color:[UIColor lightGrayColor] cornerRadius:acornerRadius];
}

-(void) addBorderWidth:(float) awidth{
	[self addBorderWidth:awidth color:[UIColor lightGrayColor] cornerRadius:0];
}


@end




#pragma mark UIImage
@implementation UIImage (iPhone5extension)
+ (UIImage*)imageNamedForDevice:(NSString*)name {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if (([UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale) == 1136.0f) {
            //Check if is there a path extension or not
            if (name.pathExtension.length) {
                name = [name stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@".%@", name.pathExtension]
                                                       withString: [NSString stringWithFormat:@"-568h@2x.%@", name.pathExtension ] ];
            } else {
                name = [name stringByAppendingString:@"-568h@2x"];
            }
            
            //load the image e.g from disk or cache
            UIImage *image = [UIImage imageNamed:name];
            if (image) {
                //strange Bug in iOS, the image name have a “@2x” but the scale isn’t 2.0f
                return [UIImage imageWithCGImage:image.CGImage scale:2.0f orientation:image.imageOrientation];
            }
        }
    }
    return [UIImage imageNamed:name];
}
@end









